package board.control;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import board.model.BoardReplyVO;
import board.model.BoardService;
import board.model.BoardVO;
import board.policy.FileUploadPolicy;

import com.oreilly.servlet.MultipartRequest;

public class BoardController extends MultiActionController{
	private BoardService boardService;
	public BoardController(BoardService boardService){
		this.boardService = boardService;
	}
	String imgfilename = "";
	private String path; // 사진파일 경로 설정을 위한.. 0424 임한희
	public void setPath(String path) {
		this.path = path;
	}
	// 게시물 목록 불러옴 0424 임한희
	public ModelAndView boardList(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		return new ModelAndView("index.funfun","lvo",boardService.boardList(request.getParameter("pageNo")));
	}
	// 게시물 삭제 0424 임한희
	public ModelAndView boardDeleteContent(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String no = request.getParameter("no");
		int replyrefno = Integer.parseInt(request.getParameter("replyrefno"));
		boardService.boardDeleteContent(replyrefno, no);
		return new ModelAndView("index.funfun", "lvo", boardService.boardList(request.getParameter("1")));
	}
	// 관리자 게시물 선택 삭제 0502 임한희
	public ModelAndView boardAdminDelete(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String [] no = request.getParameterValues("boardCk");
		for(int i=0;i<no.length;i++){
			int replyno = Integer.parseInt(no[i]);
			System.out.println("checkBox test : "+ no[i]);
			boardService.boardDeleteContent(replyno, no[i]);
		}
		return new ModelAndView("index.funfun", "lvo", boardService.boardList(request.getParameter("1")));
	}
	//게시물 내용 보는 컨트롤러 0426 박정형
	public ModelAndView boardContentView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String no = request.getParameter("no");
		BoardVO bvo=boardService.boardContentView(no);
		return new ModelAndView("boardcontentview.board", "boardVO", bvo);
	}
	//게시물 수정  컨트롤러 0426 박정형
	public ModelAndView boardUpdate(HttpServletRequest request, HttpServletResponse response, BoardVO bvo1) throws SQLException{
		BoardVO bvo=boardService.boardUpdate(bvo1);
		return new ModelAndView("boardcontentview.board", "boardVO", bvo);
	}
	// 게시물 수정폼으로 데이터 가져오기 위한 컨트롤러 0507 임한희
	public ModelAndView boardUpdateView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String no = request.getParameter("no");
		BoardVO bvo = boardService.boardUpdateView(no);
		return new ModelAndView("boardupdate.board", "boardVO", bvo);
	}
	// 리플 작성하는 컨트롤러 0430 임한희
	public ModelAndView boardReplyWriter(HttpServletRequest request, HttpServletResponse response, BoardReplyVO brvo) throws SQLException{
		System.out.println("replyrefno : "+brvo.getReplyrefno());
		boardService.boardReplyWriter(brvo);
		List<BoardReplyVO> replyList = boardService.boardReplyView(brvo.getReplyrefno());
		return new ModelAndView("JsonView", "replyList", replyList);
	}
	// 리플들 읽어오는 컨트롤러 0430 임한희
	public ModelAndView boardReplyView(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int no = Integer.parseInt(request.getParameter("replyrefno"));
		List<BoardReplyVO> replyList = boardService.boardReplyView(no);
		return new ModelAndView("JsonView", "replyList", replyList);
	}
	// 리플 삭제하는 컨트롤러 0501 임한희
	public ModelAndView boardReplyDelete(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		int replyno = Integer.parseInt(request.getParameter("replyno"));
		int replyrefno = Integer.parseInt(request.getParameter("replyrefno"));
		boardService.boardReplyDelete(replyno);
		List<BoardReplyVO> replyList = boardService.boardReplyView(replyrefno);
		return new ModelAndView("JsonView", "replyList", replyList);
	}
	// 리스트 화면 대신에 구글맵으로 표시하고 다중마커하기 위한 컨트롤러 0509 임한희
	public ModelAndView boardMapViewMarkers(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		List<BoardVO> mapList = boardService.boardMapViewMarkers();
		return new ModelAndView("JsonView", "mapList", mapList);
	}
	// gps 좌표를 가지고 해당 게시글 찾는 컨트롤러 0511 임한희
	public ModelAndView boardGetContentByGpsinfo(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String title = request.getParameter("title");
		BoardVO bvo = boardService.boardGetContentByGpsinfo(title);
		return new ModelAndView("boardcontentview.board", "boardVO", bvo);
	}
	// 매칭시 상대방 아이디로 게시글 리스트 가져오는 컨트롤러 0514 임한희
	public ModelAndView boardGetListById(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String writer = request.getParameter("writer");
		return new ModelAndView("index.funfun","lvo",boardService.boardGetListById(writer));
	}
	public ModelAndView appFileUpload(HttpServletRequest request,HttpServletResponse response){
		String folderTypePath = "C:/java-kosta/funfunproject/funfun/WebContent/uploadfiles/";
		int sizeLimit = 5 * 1024 * 1024 ; // 5메가까지 제한 넘어서면 예외발생
		MultipartRequest multi = null;
		try {
			multi = new MultipartRequest(request, folderTypePath, sizeLimit, new FileUploadPolicy());
		} catch (IOException e) {
			e.printStackTrace();
		}
		@SuppressWarnings("rawtypes")
		Enumeration files = multi.getFileNames();
		 String fileInput = "";
	     while(files.hasMoreElements()) {
	          fileInput = (String)files.nextElement();            
	          imgfilename = multi.getFilesystemName(fileInput);           
	     }
		return null;
	}
	public ModelAndView appBoardWrite(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String title = request.getParameter("title");
		String contents = request.getParameter("contents");
		String writer = request.getParameter("writer");
		String imgsrc = request.getParameter("imgsrc");
		String photoDate = request.getParameter("photoDate");
		String latitude = request.getParameter("latitude");
		String longitude = request.getParameter("longitude");
		BoardVO bvo = new BoardVO(0, title, contents, writer, imgsrc,
				imgfilename, "", photoDate, 0, latitude,
				longitude);
		boardService.boardWrite(bvo);
		return null;
	}
}
